// 角色管理模块
const {roleSys} = require('../dbConfig/index.js')
// 新增角色
const addRole = async (ctx, next) => {
	const { role_name, role_school_id, role_rank_id, role_class_ids } = ctx.request.body;
	if (!role_name || !role_school_id || !role_rank_id || !role_class_ids) {
		ctx.body = {
			code: 400,
			msg: '角色名称、所属院校、职级、所负责年级不能为空',
			data: null
		};
		return;
	}
	const result = await roleSys.create({
		role_name,
		role_school_id,
		role_rank_id,
		role_class_ids: JSON.parse(role_class_ids).join(',')
	});
	ctx.body = {
		code: 200,
		msg: '角色添加成功',
		data: result
	};	
}

// 修改角色
const updateRole = async (ctx, next) => {
	const { role_id, role_name, role_school_id, role_rank_id, role_class_ids } = ctx.request.body;
	if (!role_id || !role_name || !role_school_id || !role_rank_id || !role_class_ids) {
		ctx.body = {
			code: 400,
			msg: '角色名称、所属院校、职级、所负责年级不能为空',
			data: null
		};
		return;
	}
	const result = await roleSys.update({
		role_name,
		role_school_id,
		role_rank_id,
		role_class_ids: role_class_ids.join(',')
	}, {
		where: {
			role_id
		}
	});
	ctx.body = {
		code: 200,
		msg: '角色修改成功',
		data: result
	};	
}

// 获取角色列表
const getRoleList = async (ctx, next) => {
	const { role_school_id } = ctx.request.body;
	if (!role_school_id) {
		ctx.body = {
			code: 400,
			msg: '所属院校不能为空',
			data: null
		};
		return;
	}
	const result = await roleSys.findAll({
		where: {
			role_school_id
		}
	});
	ctx.body = {
		code: 200,
		msg: '角色获取成功',
		data: result
	};	
}

// 删除角色 TODO
const deleteRole = async (ctx, next) => {
ctx.body = {
		code: 200,
		msg: '开发中',
		data: result
	};	
}
module.exports = {
	addRole,
	updateRole,
	getRoleList,
	deleteRole
}